<?php

namespace app\vip_agent\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Agent extends Base
{
    //
    //
    public function index()
    {
//        $user = Db::name('users')->insert($post);

        $list = Db::name('users')->where('agents',Session::get('user.id'))->paginate();
//        var_dump($list);
        $page=$list->render();
        $list=$list->items();
//        foreach ($list as $k=>$v){
//            if(strlen($v['name'])==11){
//                $list[$k]['name']=substr_replace($v['name'], '*****', 3, 5);
//            }else{
//                $list[$k]['name']=substr_replace($v['name'], '*', 3, 3);
//            }
//            $list[$k]['phonenumber']=substr_replace($v['phonenumber'], '*****', 3, 5);
//        }
        return view('agent/index',['list'=>$list,'page'=>$page]);
    }

    public function add()
    {
        return view('agent/add');
    }


    public function doadd(Request $request)
    {
        $post=$request->post();
        //
        if($post['password']!=$post['repassword']){
//            return json_encode(['msg'=>'密码不一致']);
            return "<script>alert('密码不一致');history.go(-1)</script>";
        }
        unset($post['repassword']);
        $name = Db::name('users')->where('phonenumber',$post['phonenumber'])->field('phonenumber')->find();
        if($name){
//            return json_encode(['msg'=>'用户已经存在'],JSON_UNESCAPED_UNICODE);
            return "<script>alert('用户已经存在');history.go(-1)</script>";
        }else{
            $post['usercode'] =  $this->GetRandStr();//随机数
            $post['creationtime']=date('Y-m-d H:i:s');
            $post['agents']=Session::get('user.id');
            $post['register']=3;


            $user = Db::name('users')->insert($post);
            if(!$user){
//                    return json_encode(['msg'=>'注册失败'],JSON_UNESCAPED_UNICODE);
                return "<script>alert('注册失败');history.go(-1)</script>";
            }else{
//                    return json_encode(['msg'=>'success'],JSON_UNESCAPED_UNICODE);
                return "<script>alert('注册成功');window.location.href='".url('vip_agent/Agent/index')."'</script>";
            }


            // }
        }
    }
    public function GetRandStr(){
        $chars = array(
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
            "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
            "3", "4", "5", "6", "7", "8", "9"
        );
        $charsLen = count($chars) - 1;
        shuffle($chars);
        $output = "";
        for ($i=0; $i<4; $i++){
            $output .= $chars[mt_rand(0, $charsLen)];
        }
        return $output;
    }

    public function realname()
    {
        $id=Session::get('user.id');
        $list=Db::name('user_certification')->where('uid',$id)->find();
        if(!$list){
            $list['name']='';
            $list['IDnumber']='';
            $list['IDpositive']='';
            $list['IDback']='';
            $list['status']=3;
        }


        return view('agent/realname',['list'=>$list]);

    }

    // 交易记录
    public function showlist(Request $request)
    {
        $session=Session::get();
        $get=$request->get();
        $get['leaguer']=isset($get['leaguer'])?$session['user']['id']:$session['user']['id'];
        $get['agents']=isset($get['agents'])?$session['user']['id']:$session['user']['id'];
        $get['acc']=isset($get['acc'])?$get['acc']:'0';
        $get['kp']=isset($get['kp'])?$get['kp']:'开,平,平今';
        $get['mm']=isset($get['mm'])?$get['mm']:'买,卖';
        $get['hy']=isset($get['hy'])?$get['hy']:'';
        $get['zzh']=isset($get['zzh'])?$get['zzh']:'';
        $get['start']=isset($get['start'])?$get['start']:date('Y-m-d',strtotime('-1 day'));
        $get['end']=isset($get['end'])?$get['end']:date('Y-m-d');
        $jystart=str_replace('-','',$get['start']);
        $jyend=str_replace('-','',$get['end']);

        $acc=Db::name('primary_account')->field('account')->select();
        foreach ($acc as $k=>$v){
            if($v['account']==$get['acc']){
                $acc[$k]['status']=1;
            }else{
                $acc[$k]['status']=0;
            }
            $acctemp[$k]=$v['account'];
        }

        if($get['acc']==0){
            $acctemp1=$acctemp;
            $acctempop='in';
        }else{
            $acctemp1=$get['acc'];
            $acctempop='eq';
        }
        $kp=explode(',',$get['kp']);
        $mm=explode(',',$get['mm']);
        if($get['agents']==0){
            // 所有代理商
            if($get['leaguer']==0){
                $agentsall=[];
                $count['Count']=Db::name('transaction')
                    ->where('TradingDday','between',[$jystart,$jyend])
                    ->where('Contract','like','%'.$get['hy'].'%')
                    ->where('Subaccount','like','%'.$get['zzh'].'%')
                    ->where('Business','in',$mm)
                    ->where('Kaiping','in',$kp)
                    ->where('PrimaryAccount',$acctempop,$acctemp1)
                    ->order('id desc')
                    ->sum('Count');
                $count['Poundage']=Db::name('transaction')
                    ->where('TradingDday','between',[$jystart,$jyend])
                    ->where('Contract','like','%'.$get['hy'].'%')
                    ->where('Subaccount','like','%'.$get['zzh'].'%')
                    ->where('Business','in',$mm)
                    ->where('Kaiping','in',$kp)
                    ->where('PrimaryAccount',$acctempop,$acctemp1)
                    ->order('id desc')
                    ->sum('Poundage');
                $count['Float']=Db::name('transaction')
                    ->where('TradingDday','between',[$jystart,$jyend])
                    ->where('Contract','like','%'.$get['hy'].'%')
                    ->where('Subaccount','like','%'.$get['zzh'].'%')
                    ->where('Business','in',$mm)
                    ->where('Kaiping','in',$kp)
                    ->where('PrimaryAccount',$acctempop,$acctemp1)
                    ->order('id desc')
                    ->sum('`Float`');
                $count['Profit']=Db::name('transaction')
                    ->where('TradingDday','between',[$jystart,$jyend])
                    ->where('Contract','like','%'.$get['hy'].'%')
                    ->where('Subaccount','like','%'.$get['zzh'].'%')
                    ->where('Business','in',$mm)
                    ->where('Kaiping','in',$kp)
                    ->where('PrimaryAccount',$acctempop,$acctemp1)
                    ->order('id desc')
                    ->sum('Profit');
                $count['Brokerage']=Db::name('transaction')
                    ->where('TradingDday','between',[$jystart,$jyend])
                    ->where('Contract','like','%'.$get['hy'].'%')
                    ->where('Subaccount','like','%'.$get['zzh'].'%')
                    ->where('Business','in',$mm)
                    ->where('Kaiping','in',$kp)
                    ->where('PrimaryAccount',$acctempop,$acctemp1)
                    ->order('id desc')
                    ->sum('Brokerage');
                $list=Db::name('transaction')
                    ->where('TradingDday','between',[$jystart,$jyend])
                    ->where('Contract','like','%'.$get['hy'].'%')
                    ->where('Subaccount','like','%'.$get['zzh'].'%')
                    ->where('Business','in',$mm)
                    ->where('Kaiping','in',$kp)
                    ->where('PrimaryAccount',$acctempop,$acctemp1)
                    ->order('id desc')
                    ->paginate(15,false,['query'=>$get]);
                $page=$list->render();
                $list=$list->items();
                return view('transaction/showlist1',['get'=>$get,'list'=>$list,'page'=>$page,'agents'=>$agentsall,'acc'=>$acc,'count'=>$count]);
            }else{
                // 指定企业会员
                $agentsall=Db::name('users')->field('id,name')->where('register',2)->where('leaguer',$get['leaguer'])->select();
                foreach ($agentsall as $k=>$v){
                    if($v['id']==$get['agents']){
                        $agentsall[$k]['status']=1;
                    }else{
                        $agentsall[$k]['status']=0;
                    }
                }
                $count['Count']=Db::name('transaction')
                    ->where('TradingDday','between',[$jystart,$jyend])
                    ->where('Contract','like','%'.$get['hy'].'%')
                    ->where('Subaccount','like','%'.$get['zzh'].'%')
                    ->where('Business','in',$mm)
                    ->where('Kaiping','in',$kp)
                    ->where('PrimaryAccount',$acctempop,$acctemp1)
                    ->where('vip',$get['leaguer'])
                    ->order('id desc')
                    ->sum('Count');
                $count['Poundage']=Db::name('transaction')
                    ->where('TradingDday','between',[$jystart,$jyend])
                    ->where('Contract','like','%'.$get['hy'].'%')
                    ->where('Subaccount','like','%'.$get['zzh'].'%')
                    ->where('Business','in',$mm)
                    ->where('Kaiping','in',$kp)
                    ->where('PrimaryAccount',$acctempop,$acctemp1)
                    ->where('vip',$get['leaguer'])
                    ->order('id desc')
                    ->sum('Poundage');
                $count['Float']=Db::name('transaction')
                    ->where('TradingDday','between',[$jystart,$jyend])
                    ->where('Contract','like','%'.$get['hy'].'%')
                    ->where('Subaccount','like','%'.$get['zzh'].'%')
                    ->where('Business','in',$mm)
                    ->where('Kaiping','in',$kp)
                    ->where('PrimaryAccount',$acctempop,$acctemp1)
                    ->where('vip',$get['leaguer'])
                    ->order('id desc')
                    ->sum('`Float`');
                $count['Profit']=Db::name('transaction')
                    ->where('TradingDday','between',[$jystart,$jyend])
                    ->where('Contract','like','%'.$get['hy'].'%')
                    ->where('Subaccount','like','%'.$get['zzh'].'%')
                    ->where('Business','in',$mm)
                    ->where('Kaiping','in',$kp)
                    ->where('PrimaryAccount',$acctempop,$acctemp1)
                    ->where('vip',$get['leaguer'])
                    ->order('id desc')
                    ->sum('Profit');
                $count['Brokerage']=Db::name('transaction')
                    ->where('TradingDday','between',[$jystart,$jyend])
                    ->where('Contract','like','%'.$get['hy'].'%')
                    ->where('Subaccount','like','%'.$get['zzh'].'%')
                    ->where('Business','in',$mm)
                    ->where('Kaiping','in',$kp)
                    ->where('PrimaryAccount',$acctempop,$acctemp1)
                    ->where('vip',$get['leaguer'])
                    ->order('id desc')
                    ->sum('Brokerage');


                $list=Db::name('transaction')
                    ->where('TradingDday','between',[$jystart,$jyend])
                    ->where('Contract','like','%'.$get['hy'].'%')
                    ->where('Subaccount','like','%'.$get['zzh'].'%')
                    ->where('Business','in',$mm)
                    ->where('Kaiping','in',$kp)
                    ->where('PrimaryAccount',$acctempop,$acctemp1)
                    ->where('vip',$get['leaguer'])
                    ->order('id desc')
                    ->paginate(15,false,['query'=>$get]);
                $page=$list->render();
                $list=$list->items();
                return view('transaction/showlist1',['get'=>$get,'list'=>$list,'page'=>$page,'agents'=>$agentsall,'acc'=>$acc,'count'=>$count]);
            }
        }else{
            // 制定代理商
            $agentsall=Db::name('users')->field('id,name')->where('register',2)->where('leaguer',$get['leaguer'])->select();
            foreach ($agentsall as $k=>$v){
                if($v['id']==$get['agents']){
                    $agentsall[$k]['status']=1;
                }else{
                    $agentsall[$k]['status']=0;
                }
            }
            $count['Count']=Db::name('transaction')
                ->where('TradingDday','between',[$jystart,$jyend])
                ->where('Contract','like','%'.$get['hy'].'%')
                ->where('Subaccount','like','%'.$get['zzh'].'%')
                ->where('Business','in',$mm)
                ->where('Kaiping','in',$kp)
                ->where('PrimaryAccount',$acctempop,$acctemp1)
                ->where('agents',$get['agents'])
                ->order('id desc')
                ->sum('Count');
            $count['Poundage']=Db::name('transaction')
                ->where('TradingDday','between',[$jystart,$jyend])
                ->where('Contract','like','%'.$get['hy'].'%')
                ->where('Subaccount','like','%'.$get['zzh'].'%')
                ->where('Business','in',$mm)
                ->where('Kaiping','in',$kp)
                ->where('PrimaryAccount',$acctempop,$acctemp1)
                ->where('agents',$get['agents'])
                ->order('id desc')
                ->sum('Poundage');
            $count['Float']=Db::name('transaction')
                ->where('TradingDday','between',[$jystart,$jyend])
                ->where('Contract','like','%'.$get['hy'].'%')
                ->where('Subaccount','like','%'.$get['zzh'].'%')
                ->where('Business','in',$mm)
                ->where('Kaiping','in',$kp)
                ->where('PrimaryAccount',$acctempop,$acctemp1)
                ->where('agents',$get['agents'])
                ->order('id desc')
                ->sum('`Float`');
            $count['Profit']=Db::name('transaction')
                ->where('TradingDday','between',[$jystart,$jyend])
                ->where('Contract','like','%'.$get['hy'].'%')
                ->where('Subaccount','like','%'.$get['zzh'].'%')
                ->where('Business','in',$mm)
                ->where('Kaiping','in',$kp)
                ->where('PrimaryAccount',$acctempop,$acctemp1)
                ->where('agents',$get['agents'])
                ->order('id desc')
                ->sum('Profit');
            $count['Brokerage']=Db::name('transaction')
                ->where('TradingDday','between',[$jystart,$jyend])
                ->where('Contract','like','%'.$get['hy'].'%')
                ->where('Subaccount','like','%'.$get['zzh'].'%')
                ->where('Business','in',$mm)
                ->where('Kaiping','in',$kp)
                ->where('PrimaryAccount',$acctempop,$acctemp1)
                ->where('agents',$get['agents'])
                ->order('id desc')
                ->sum('Brokerage');


            $list=Db::name('transaction')
                ->where('TradingDday','between',[$jystart,$jyend])
                ->where('Contract','like','%'.$get['hy'].'%')
                ->where('Subaccount','like','%'.$get['zzh'].'%')
                ->where('Business','in',$mm)
                ->where('Kaiping','in',$kp)
                ->where('PrimaryAccount',$acctempop,$acctemp1)
                ->where('agents',$get['agents'])
                ->order('id desc')
                ->paginate(15,false,['query'=>$get]);
            $page=$list->render();
            $list=$list->items();
            return view('transaction/showlist1',['get'=>$get,'list'=>$list,'page'=>$page,'agents'=>$agentsall,'acc'=>$acc,'count'=>$count]);
        }

    }


    // 出入金统计日期页
    public function crsum()
    {
        $get['start']=isset($get['start'])?$get['start']:date('Y-m-d',strtotime('monday last week'));
        $get['end']=isset($get['end'])?$get['end']:date('Y-m-d');
        return view('agent/showsum',['get'=>$get]);
    }
    public function crsummary(Request $request)
    {
        $get=$request->get();
        $aid=Session::get('user.id');
//        $aid=12505;
        $get['start']=isset($get['start'])?$get['start']:date('Y-m-d',strtotime('monday last week'));
        $get['end']=isset($get['end'])?$get['end']:date('Y-m-d',strtotime("friday last week"));
        $get['name']=isset($get['name'])?trim($get['name']):'';
        $jystart=str_replace('-','',$get['start']);
//        $jyend=date('Y-m-d',strtotime($get['end'].'+1 day'));
        $jyend=str_replace('-','',$get['end']);
        $jystart=date('Y-m-d H:i:s',strtotime($jystart)-3600*8);
        $jyend=date('Y-m-d H:i:s',strtotime($jyend)+3600*16);

//        $viplist=Db::name('users')->field('id,name')->where('register',1)->paginate(15,false,['query'=>$get]);
//        $page=$viplist->render();
//        $list=$viplist->items();
//        foreach ($list as $k=>$v){
//            $list[$k]['rj']=0;
//            $temp=Db::name('users')->field('id')->where('register',3)->where('leaguer',$v['id'])->column('id');
//            foreach ($temp as $key=>$value){
//                $list[$k]['rj']+=abs(Db::name('cashlog')->where('uid',$value)->where('time','between',[$jystart,$jyend])->where('type',5)->sum('price'));
//            }
//        }

//        $userlist=Db::name('users')->field('id,name')->where('name','like','%'.$get['name'].'%')->where('agents',$aid)->paginate(15,false,['query'=>$get]);
//        $page=$userlist->render();
//        $list=$userlist->items();
        $userall=Db::name('users')->field('id')->where('name','like','%'.$get['name'].'%')->where('agents',$aid)->column('id');

        $list=Db::name('cashlog')
            ->alias('a')
            ->join('lc_users b','a.uid=b.id')
            ->field('a.*,b.name')
            ->where('a.uid','in',$userall)
            ->where('type','in','5,6')
            ->where('b.name','like','%'.$get['name'].'%')
            ->where('time','between',[$jystart,$jyend])
            ->paginate(15,false,['query'=>$get]);


        $page=$list->render();
        $list=$list->items();
//        foreach ($list as $k=>$v){
//            $list[$k]['rj']=Db::name('cashlog')->where('time','between',[$jystart,$jyend])->where('uid',$v['id'])->where('type',5)->sum('price');
//            $list[$k]['cj']=Db::name('cashlog')->where('time','between',[$jystart,$jyend])->where('uid',$v['id'])->where('type',6)->sum('price');
//        }
        $tj['rj']=Db::name('cashlog')->where('time','between',[$jystart,$jyend])->where('uid','in',$userall)->where('type',5)->sum('price');
        $tj['cj']=Db::name('cashlog')->where('time','between',[$jystart,$jyend])->where('uid','in',$userall)->where('type',6)->sum('price');
        return view('agent/showsummary',['list'=>$list,'page'=>$page,'get'=>$get,'tj'=>$tj]);
    }



}
